from rest_framework import serializers
from store.models import TinyShop, Order, Product, GoodsCategory, GoodsLib, ProductImage, TinyshopImage
from module.models import Module
from sdk.models import PreparedProduct
from flagship.models import Flagship
import settings
from api.middlewares import ThreadLocal
from feedback.models import Feedback
from faq.models import PhoneHelp
import json
from customer.models import Address


class TinyShopSerializer(serializers.ModelSerializer):
    image_src = serializers.SerializerMethodField('get_image_src')
    def get_image_src(self, obj):
        if obj.image:
            return "%s%s" % (settings.MEDIA_URL, obj.image.image)
        return None

    image_id = serializers.SerializerMethodField('get_image_id')
    def get_image_id(self, obj):
        return obj.image_id
    
    account = serializers.SerializerMethodField('get_account')
    def get_account(self, obj):
        if self.context.get('request') and self.context.get('request').session.get("mobile_no") == obj.mobile_no:
            return obj.account
        return "0"

    sales = serializers.SerializerMethodField('get_sales')
    def get_sales(self, obj):
        if self.context.get('request') and self.context.get('request').session.get("mobile_no") == obj.mobile_no:
            return obj.sales
        return "0"
    
    class Meta:
        model = TinyShop
        fields = ('id', 'name', 'mobile_no', 'image_src', 'account', 'sales', 'image_id')
        
class FeedbackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Feedback
        
class OrderSerializer(serializers.ModelSerializer):
    
    def get_address(self, obj):
        if obj.address == None:
            return None
        try:
          json_object = json.loads(obj.address)
          return json_object
        except ValueError, e:
          return None
    
    def get_product_count(self, obj):
        amount = 0
        
        if obj.detail == None:
            return amount
        try:
          parr = json.loads(obj.detail)
          for i in parr:
              amount += i[2] 
        except ValueError, e:
          return None
        return amount
    
    def get_mobile_no(self, obj):
        return obj.tinyshop.mobile_no
      
    address = serializers.SerializerMethodField('get_address')
    product_count = serializers.SerializerMethodField('get_product_count')
    mobile_no = serializers.SerializerMethodField('get_mobile_no')
    
    class Meta:
        model = Order
        fields = ('id', 'tinyshop', 'mobile_no', 'created_at', 'amount', 'product_amount', 'payer', 'detail', 'payment', 'needexpress', 'address', 'status', 'trans_id')

class TinyshopImageSerializer(serializers.ModelSerializer):
    image = serializers.ImageField()
    class Meta:
        model = TinyshopImage

class ProductImageSerializer(serializers.ModelSerializer):
    image = serializers.ImageField()
    class Meta:
        model = ProductImage

class PreparedProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = PreparedProduct
        
class ProductSerializer(serializers.ModelSerializer):
    image_src = serializers.SerializerMethodField('get_image_src')

    def get_image_src(self, obj):
        if obj.image == None:
            return None
        s = "%s" % obj.image.image
        if s.startswith("http"):
            return s
        else:
            return "%s%s" % (settings.MEDIA_URL, s)
    
    class Meta:
        model = Product
        fields = ('id', 'name', 'tinyshop', 'image', 'image_src', 'price', 'status', 'information')

class GoodsCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory

class GoodsLibSerializer(serializers.ModelSerializer):
    image_src = serializers.SerializerMethodField('get_image_src')
    def get_image_src(self, obj):
        return "%sstore/category/%d.jpg" % (settings.MEDIA_URL, obj.pk)
    image_id = serializers.SerializerMethodField('get_image_id')
    def get_image_id(self, obj):
        return obj.pk

    class Meta:
        model = GoodsLib
        fields = ('id', 'name', 'category', 'price', 'image_src', 'information', 'image_id')


class ModuleSerializer(serializers.ModelSerializer):
    icon = serializers.Field(source='icon')
    
    class Meta:
        model = Module

class AddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = Address

class FlagshipSerializer(serializers.ModelSerializer):
    class Meta:
        model = Flagship

class PhoneHelpSerializer(serializers.ModelSerializer):
    class Meta:
        model = PhoneHelp
